package com.xmut.service.impl;

import com.xmut.mapper.UserMapper;
import com.xmut.pojo.User;
import com.xmut.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Boolean rigister(User user) {
        List<User> users = userMapper.getAllUser();//获得所有的用户信息
        String username = user.getUsername();
        for (User key:users)
        {
            if(username.equals(key.getUsername())||username.isEmpty())
            {
                return false;
            }
        }
        String md5Pass = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(md5Pass);

        if (user.getIsadmin().equals("2217"))
        {
            user.setIsadmin("1");
        }
        else
        {
            user.setIsadmin("0");
        }
        userMapper.rigister(user);
        return true;
    }

    @Override
    public List<User> getAllUser() {

        return userMapper.getAllUser();
    }

    @Override
    public Boolean login(User user) {

        String username = user.getUsername();
        String password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        User user1 = userMapper.getUserByName(username);
        if (user1 == null)
        {
            return false;
        }
        else
        {
            if(password.equals(user1.getPassword()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    @Override
    public User getUserByName(User user) {
        return userMapper.getUserByName(user.getUsername());
    }

    @Override
    public User getVolunteerInfo(Integer id) {

        return userMapper.getVolunteerInfo(id);
    }

    @Override
    public void insVolunteerAddress(User user) {
        userMapper.insVolunteerAddress(user);
    }

    @Override
    public void setVolunteer(Integer id) {
        userMapper.setVolunteer(id);
    }
}
